Issue using Python to solve the Coin problem [closed]

Posted by challarao on Stack Overflow See other posts from Stack Overflow or by challarao
Published on 2010-06-17T05:05:53Z Indexed on 2010/06/17 5:43 UTC
Read the original article Hit count: 193

Filed under:
|

I'm attempting to solve a problem commonly known as the Coin problem, but using McNuggets. McNuggets come in boxes containing either 6, 9, or 20 nuggets. I want to write a python script that uses Diophantine equations to determine if a given number of McNuggets n can be exactly purchased in these groupings. For example:

  • 26 McNuggets -- Possible: 1 6-pack, 0 9-packs, 1 20-pack
  • 27 McNuggets -- Possible: 0 6-packs, 3 9-packs, 0 20-packs
  • 28 McNuggets -- Not possible

This is my current attempt at writing the solution in Python, but the output is incorrect and I'm not sure what's wrong.

n=input("Enter the no.of McNuggets:")   
a,b,c=0,0,0   
count=0   
for a in range(n):  
    if 6*a+9*b+20*c==n:     
            count=count+1
               break
    else:  
        for b in range(n):  
            if 6*a+9*b+20*c==n:  
                count=count+1  
                break  
            else:  
                for c in range(n):  
                    if 6*a+9*b+20*c==n:  
                        count=count+1  
                        break  
if count>0:  
    print "It is possible to buy exactly",n,"packs of McNuggetss",a,b,c  
else:  
    print "It is not possible to buy"  

© Stack Overflow or respective owner

Related posts about python

Related posts about diophantine

  • Solving Diophantine Equations Using Python

    as seen on Stack Overflow - Search for 'Stack Overflow'
    In mathematics, a Diophantine equation (named for Diophantus of Alexandria, a third century Greek mathematician) is a polynomial equation where the variables can only take on integer values. Although you may not realize it, you have seen Diophantine equations before: one of the most famous Diophantine… >>> More

  • Diophantine Equation [closed]

    as seen on Stack Overflow - Search for 'Stack Overflow'
    In mathematics, a Diophantine equation (named for Diophantus of Alexandria, a third century Greek mathematician) is a polynomial equation where the variables can only take on integer values. Although you may not realize it, you have seen Diophantine equations before: one of the most famous Diophantine… >>> More

  • Diophantine Equation

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Show that it is possible to buy exactly 50, 51, 52, 53, 54, and 55 McNuggets, by finding solutions to the Diophantine equation. You can solve this in your head, using paper and pencil, or writing a program. However you chose to solve this problem, list the combinations of 6, 9 and 20 packs of McNuggets… >>> More

  • diophantine equation

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Write an iterative program that finds the largest number of McNuggets that cannot be bought in exact quantity. Your program should print the answer in the following format (where the correct number is provided in place of n): "Largest number of McNuggets that cannot be bought in exact quantity: n" in… >>> More

  • Diophantine Equation

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Write an iterative program that finds the largest number of McNuggets that cannot be bought in exact quantity. Your program should print the answer in the following format (where the correct number is provided in place of n): "Largest number of McNuggets that cannot be bought in exact quantity: n" Hints: Hypothesize… >>> More